/*
 * SimulatorGUI1.java
 *
 * Created on March 5, 2008, 1:37 PM
 */

package my.SimulatorGUI1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.logging.Level;
import java.util.logging.Logger;
import my.util.Debug;
import java.text.DecimalFormat;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;



/**
 *
 * @author  rp012053
 */
public class SimulatorGUI1 extends javax.swing.JFrame  {
    
    private static int MAXBATTERY = 100;
    private static int MINBATTERY = 0;
    private static int MAXBLOODSUGAR = 35;
    private static int MINBLOODSUGAR = 0;
    private static int MAXINSULIN = 100;
    private static int MININSULIN = 0;
    public static final int TIMEZONE = -7;
    
    public BSLGraph bgraph;
    
    boolean enabled = false;
    boolean powerOnDose = true;
    static LogicController callBack;
    DecimalFormat BSLFormat = new DecimalFormat("0.00");
    
    /** Creates new form SimulatorGUI1 */
    public SimulatorGUI1(LogicController cb) {
        callBack = cb;
        initComponents();
        HardwarePane.setVisible(false);
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //btnManualDose.addActionListener(this);
        
                
        //creates the graph with default visibilty of false
        bgraph = new BSLGraph();
        bgraph.setVisible(false);
        showGraphButton.setEnabled(false);
        
    }
    public long getCurrentTime(){
        return Long.parseLong(lblCurrentTime.getText());
    }
    
    public void setCurrentTime(){
        long time = System.currentTimeMillis()/1000;
        //time %= 86400;
        time += (TIMEZONE*3600);
        lblCurrentTime.setText(convertSeconds(time));  
    }
    public long getResetTime(){
        return Long.parseLong(lblResetTime.getText());
    }
    
    public void setResetTime(long time){
        lblResetTime.setText(convertSeconds(time));  
    }
    public void setOKLight(boolean b) {
        if (b == true) {
            btnGreen.setBackground(new Color(0,255,0));
        }
        else {
            btnGreen.setBackground(new Color(238,238,238));
        }
    }

    public void setErrorLight(boolean b) {
        if (b == true) {
            btnRed.setBackground(new Color(255,0,0));
        }
        else {
            btnRed.setBackground(new Color(238,238,238));
        }
    }

    public void setWarnLight(boolean b) {
        if (b == true) {
            btnYellow.setBackground(new Color(255,255,0));
        }
        else {
            btnYellow.setBackground(new Color(238,238,238));
        }
    }
    
    public String convertSeconds(long time){
        long hours, mins, secs;
        String ampm = "";
        String str ="";
        secs = time % 60;
        time /= 60;
        mins = time % 60;
        time /= 60;
        if(time > 0){
            ampm = "am";
            hours = time%24;
            if(hours > 12){
                hours -= 12;
                ampm = "pm";
            }else if(hours == 12){
                ampm = "pm";
            }else if(hours == 0){
                hours = 12;
            }
            str += hours + ":";
        }
        str += (mins < 10)? "0":"";
        str += mins + ":";
        str += (secs < 10)? "0":"";
        str += secs;
        str += ampm;
        return str;
    }
    
    
    public void enableButtons(){
        this.showGraphButton.setEnabled(true);
        this.btnManual.setEnabled(true);
        this.chkNeedle.setEnabled(true);
        this.chkPump.setEnabled(true);
        this.chkReservoir.setEnabled(true);
        this.chkSensor.setEnabled(true);
        this.txtBloodSugar.setEnabled(true);
        this.setLblCurrentBattery("100");
        
    }
    
    public void disableButtons(){
        this.showGraphButton.setEnabled(false);
        this.btnManual.setEnabled(false);
        this.chkNeedle.setEnabled(false);
        this.chkPump.setEnabled(false);
        this.chkReservoir.setEnabled(false);
        this.chkSensor.setEnabled(false);
        this.txtBloodSugar.setEnabled(false);
        this.btnManualDose.setEnabled(false);
        this.setLblCurrentBattery("");
        
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        ModePanel = new javax.swing.JPanel();
        btnManual = new javax.swing.JButton();
        btnManualDose = new javax.swing.JButton();
        showGraphButton = new javax.swing.JButton();
        btnSetHWStates = new javax.swing.JButton();
        HardwarePane = new javax.swing.JPanel();
        chkNeedle = new javax.swing.JCheckBox();
        chkPump = new javax.swing.JCheckBox();
        chkSensor = new javax.swing.JCheckBox();
        chkReservoir = new javax.swing.JCheckBox();
        LevelsPane = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cptCurrent = new javax.swing.JLabel();
        cptPrev1 = new javax.swing.JLabel();
        cptPrev2 = new javax.swing.JLabel();
        lblBloodSugar = new javax.swing.JLabel();
        txtBloodSugar = new javax.swing.JTextField();
        lblCurrent = new javax.swing.JLabel();
        lblPrev1 = new javax.swing.JLabel();
        lblPrev2 = new javax.swing.JLabel();
        lblCurrentBattery = new javax.swing.JLabel();
        lblBattery = new javax.swing.JLabel();
        BatteryBar = new javax.swing.JProgressBar();
        lblCurrentInsulin = new javax.swing.JLabel();
        lblInsulin = new javax.swing.JLabel();
        InsulinBar = new javax.swing.JProgressBar();
        lblInsulinDose = new javax.swing.JLabel();
        lblDoseTime = new javax.swing.JLabel();
        lblLastDose = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblLastDoseTime = new javax.swing.JLabel();
        PowerPane = new javax.swing.JPanel();
        btnPower = new javax.swing.JToggleButton();
        TimePane = new javax.swing.JPanel();
        cptResetTime = new javax.swing.JLabel();
        cptCurrentTime = new javax.swing.JLabel();
        lblResetTime = new javax.swing.JLabel();
        lblCurrentTime = new javax.swing.JLabel();
        lblError = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnRed = new javax.swing.JButton();
        btnYellow = new javax.swing.JButton();
        btnGreen = new javax.swing.JButton();

        jRadioButton1.setText("jRadioButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        ModePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Set Mode"));
        ModePanel.setName("Set Mode"); // NOI18N
        ModePanel.setLayout(new java.awt.GridLayout(1, 0, 50, 0));

        btnManual.setText("Manual");
        btnManual.setEnabled(false);
        btnManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnManualActionPerformed(evt);
            }
        });
        ModePanel.add(btnManual);

        btnManualDose.setText("Give Dose");
        btnManualDose.setEnabled(false);
        btnManualDose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnManualDoseActionPerformed(evt);
            }
        });
        ModePanel.add(btnManualDose);

        showGraphButton.setText("Show Graph");
        showGraphButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showGraphButtonActionPerformed(evt);
            }
        });
        showGraphButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                showGraphButtonKeyPressed(evt);
            }
        });
        ModePanel.add(showGraphButton);

        btnSetHWStates.setText("Set Hardware States");
        btnSetHWStates.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetHWStatesActionPerformed(evt);
            }
        });
        ModePanel.add(btnSetHWStates);

        HardwarePane.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("")), "Set Hardware Status"));
        HardwarePane.setLayout(new java.awt.GridLayout(2, 2));

        chkNeedle.setFont(new java.awt.Font("Tahoma", 0, 12));
        chkNeedle.setSelected(true);
        chkNeedle.setText("Needle Inserted");
        chkNeedle.setEnabled(false);
        chkNeedle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkNeedleActionPerformed(evt);
            }
        });
        HardwarePane.add(chkNeedle);

        chkPump.setFont(new java.awt.Font("Tahoma", 0, 12));
        chkPump.setSelected(true);
        chkPump.setText("Pump Functioning");
        chkPump.setEnabled(false);
        chkPump.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkPumpActionPerformed(evt);
            }
        });
        HardwarePane.add(chkPump);

        chkSensor.setFont(new java.awt.Font("Tahoma", 0, 12));
        chkSensor.setSelected(true);
        chkSensor.setText("Blood Sugar Sensor Functioning");
        chkSensor.setEnabled(false);
        chkSensor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkSensorActionPerformed(evt);
            }
        });
        HardwarePane.add(chkSensor);

        chkReservoir.setFont(new java.awt.Font("Tahoma", 0, 12));
        chkReservoir.setSelected(true);
        chkReservoir.setText("Insulin Reservoir Functioning");
        chkReservoir.setEnabled(false);
        chkReservoir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkReservoirActionPerformed(evt);
            }
        });
        HardwarePane.add(chkReservoir);

        LevelsPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Set Levels"));

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel2.setText("        Input");

        cptCurrent.setFont(new java.awt.Font("Tahoma", 1, 12));
        cptCurrent.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cptCurrent.setText("Current");

        cptPrev1.setFont(new java.awt.Font("Tahoma", 1, 12));
        cptPrev1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cptPrev1.setText("Previous 1");

        cptPrev2.setFont(new java.awt.Font("Tahoma", 1, 12));
        cptPrev2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cptPrev2.setText("Previous 2");

        lblBloodSugar.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblBloodSugar.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblBloodSugar.setText("Blood Sugar:");

        txtBloodSugar.setEnabled(false);
        txtBloodSugar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBloodSugarActionPerformed(evt);
            }
        });
        txtBloodSugar.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txtBloodSugarInputMethodTextChanged(evt);
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
        });

        lblCurrent.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblCurrent.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCurrent.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblPrev1.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPrev1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPrev1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblPrev2.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPrev2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPrev2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblCurrentBattery.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblCurrentBattery.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCurrentBattery.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblBattery.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblBattery.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblBattery.setText("Battery Level:");

        BatteryBar.setForeground(new java.awt.Color(0, 116, 123));

        lblCurrentInsulin.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblCurrentInsulin.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCurrentInsulin.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblInsulin.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblInsulin.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblInsulin.setText("Insulin Level:");

        InsulinBar.setForeground(new java.awt.Color(0, 255, 0));

        lblInsulinDose.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblInsulinDose.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblInsulinDose.setText("Last Dose:");

        lblDoseTime.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblDoseTime.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblDoseTime.setText("Last Dose Time:");

        lblLastDose.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLastDose.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblLastDoseTime.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblLastDoseTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLastDoseTime.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        org.jdesktop.layout.GroupLayout LevelsPaneLayout = new org.jdesktop.layout.GroupLayout(LevelsPane);
        LevelsPane.setLayout(LevelsPaneLayout);
        LevelsPaneLayout.setHorizontalGroup(
            LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LevelsPaneLayout.createSequentialGroup()
                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cptCurrent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblBattery, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblInsulin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblCurrentInsulin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblCurrentBattery, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(lblBloodSugar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(lblCurrent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(lblInsulinDose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(lblLastDose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(43, 43, 43)
                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblPrev1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(cptPrev1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblDoseTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(10, 10, 10)
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(LevelsPaneLayout.createSequentialGroup()
                                .add(18, 18, 18)
                                .add(lblLastDoseTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(223, 223, 223)
                                .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(10, 10, 10)
                                .add(jLabel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(LevelsPaneLayout.createSequentialGroup()
                                .add(18, 18, 18)
                                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(cptPrev2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(lblPrev2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(18, 18, 18)
                                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(txtBloodSugar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                    .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, InsulinBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, BatteryBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)))
                .addContainerGap())
        );
        LevelsPaneLayout.setVerticalGroup(
            LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LevelsPaneLayout.createSequentialGroup()
                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(cptCurrent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(cptPrev2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(cptPrev1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(lblBloodSugar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(lblCurrent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(lblPrev1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(lblPrev2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, txtBloodSugar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(8, 8, 8)
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblBattery, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblCurrentBattery, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblInsulin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblCurrentInsulin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(BatteryBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(InsulinBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(6, 6, 6)
                .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(lblInsulinDose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(lblLastDose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                    .add(LevelsPaneLayout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(LevelsPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblDoseTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblLastDoseTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        PowerPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Power"));

        btnPower.setText("Power");
        btnPower.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPowerActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout PowerPaneLayout = new org.jdesktop.layout.GroupLayout(PowerPane);
        PowerPane.setLayout(PowerPaneLayout);
        PowerPaneLayout.setHorizontalGroup(
            PowerPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(btnPower)
        );
        PowerPaneLayout.setVerticalGroup(
            PowerPaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(btnPower)
        );

        TimePane.setBorder(javax.swing.BorderFactory.createTitledBorder("Time"));

        cptResetTime.setFont(new java.awt.Font("Tahoma", 1, 12));
        cptResetTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cptResetTime.setText("Time Running");

        cptCurrentTime.setFont(new java.awt.Font("Tahoma", 1, 12));
        cptCurrentTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cptCurrentTime.setText("Current Time");

        lblResetTime.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblResetTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblResetTime.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblCurrentTime.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblCurrentTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCurrentTime.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        org.jdesktop.layout.GroupLayout TimePaneLayout = new org.jdesktop.layout.GroupLayout(TimePane);
        TimePane.setLayout(TimePaneLayout);
        TimePaneLayout.setHorizontalGroup(
            TimePaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TimePaneLayout.createSequentialGroup()
                .add(TimePaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(TimePaneLayout.createSequentialGroup()
                        .add(cptResetTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(cptCurrentTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(TimePaneLayout.createSequentialGroup()
                        .add(lblResetTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(10, 10, 10)
                        .add(lblCurrentTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(7, Short.MAX_VALUE))
        );
        TimePaneLayout.setVerticalGroup(
            TimePaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TimePaneLayout.createSequentialGroup()
                .add(TimePaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cptResetTime)
                    .add(cptCurrentTime))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(TimePaneLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblResetTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(lblCurrentTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        lblError.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblError.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lblError.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                lblErrorFocusLost(evt);
            }
        });

        jLabel8.setText("ERROR/WARNING:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Status"));

        btnRed.setEnabled(false);
        btnRed.setPreferredSize(new java.awt.Dimension(12, 12));
        btnRed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRedActionPerformed(evt);
            }
        });

        btnYellow.setEnabled(false);
        btnYellow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnYellowActionPerformed(evt);
            }
        });

        btnGreen.setEnabled(false);
        btnGreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGreenActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(btnRed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnYellow, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnGreen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, Short.MAX_VALUE)
                .add(17, 17, 17))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(btnRed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, Short.MAX_VALUE)
                    .add(btnYellow, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnGreen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        btnRed.getAccessibleContext().setAccessibleName("btnRed");
        btnYellow.getAccessibleContext().setAccessibleName("btnYellow");
        btnGreen.getAccessibleContext().setAccessibleName("btnGreen");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel8)
                                .add(405, 405, 405))
                            .add(lblError, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 708, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, LevelsPane, 0, 828, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, LevelsPane, 0, 828, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(PowerPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(TimePane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 249, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, ModePanel, 0, 0, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, HardwarePane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 593, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(8, 8, 8)
                        .add(jLabel8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lblError, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(9, 9, 9)
                .add(LevelsPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(ModePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(HardwarePane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(PowerPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(TimePane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void btnPowerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPowerActionPerformed
        if(enabled){
           disableButtons();
           enabled = false;
           callBack.handle("POWEROFF");
        }else{
           enableButtons();
           enabled = true;
           callBack.handle("POWERON");
        }        
    }//GEN-LAST:event_btnPowerActionPerformed
    private void txtBloodSugarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBloodSugarActionPerformed
        //INPUT: bloodsugar
        String s = txtBloodSugar.getText().trim();
        //TODO: check with non-integer data
        if (!s.equals("")) { 
            float f = Float.parseFloat(s);

            if (f < MINBLOODSUGAR) {
                JOptionPane.showMessageDialog(null,
                   f + " is below Min blood sugar: " + MINBLOODSUGAR);
            }
            else if (f > MAXBLOODSUGAR) {
                JOptionPane.showMessageDialog(null,
                   f + " is above Max blood sugar: " + MAXBLOODSUGAR);          
            }
            else {
                callBack.handle("BLOOD_SUGAR",f);
            }
            txtBloodSugar.setText("");
        }
    }//GEN-LAST:event_txtBloodSugarActionPerformed

    private void btnManualDoseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnManualDoseActionPerformed
        callBack.handle("GIVE_DOSE_CLICK");
    }//GEN-LAST:event_btnManualDoseActionPerformed

    private void lblErrorFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_lblErrorFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_lblErrorFocusLost

    private void chkNeedleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkNeedleActionPerformed
        callBack.handle("ERR_NEEDLE_OUT", !chkNeedle.isSelected());
    }//GEN-LAST:event_chkNeedleActionPerformed

    private void chkPumpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkPumpActionPerformed
        callBack.handle("ERR_PUMP_MALFUNCTION", !chkPump.isSelected());
    }//GEN-LAST:event_chkPumpActionPerformed

    private void chkSensorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkSensorActionPerformed
        callBack.handle("ERR_SUGAR_SENSOR_MALF", !chkSensor.isSelected());
    }//GEN-LAST:event_chkSensorActionPerformed

    private void chkReservoirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkReservoirActionPerformed
        callBack.handle("ERR_RESERVOIR_MISSING", !chkReservoir.isSelected());
    }//GEN-LAST:event_chkReservoirActionPerformed

    private void btnSetHWStatesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetHWStatesActionPerformed
           HardwarePane.setVisible(!HardwarePane.isVisible());
    }//GEN-LAST:event_btnSetHWStatesActionPerformed

    private void btnManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnManualActionPerformed
        callBack.handle("START_MANUAL");
    }//GEN-LAST:event_btnManualActionPerformed

    private void showGraphButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showGraphButtonActionPerformed
        bgraph.setVisible(!bgraph.isVisible());
}//GEN-LAST:event_showGraphButtonActionPerformed

    private void showGraphButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_showGraphButtonKeyPressed
        bgraph.setVisible(!bgraph.isVisible());
}//GEN-LAST:event_showGraphButtonKeyPressed
    private void btnRedActionPerformed(java.awt.event.ActionEvent evt) {                                         
    }
    private void btnGreenActionPerformed(java.awt.event.ActionEvent evt) {                                         
    }
    private void btnYellowActionPerformed(java.awt.event.ActionEvent evt) {                                         
    }
    private void txtBloodSugarInputMethodTextChanged(java.awt.event.InputMethodEvent evt){
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar BatteryBar;
    private javax.swing.JPanel HardwarePane;
    private javax.swing.JProgressBar InsulinBar;
    private javax.swing.JPanel LevelsPane;
    private javax.swing.JPanel ModePanel;
    private javax.swing.JPanel PowerPane;
    private javax.swing.JPanel TimePane;
    private javax.swing.JButton btnGreen;
    private javax.swing.JButton btnManual;
    private javax.swing.JButton btnManualDose;
    private javax.swing.JToggleButton btnPower;
    private javax.swing.JButton btnRed;
    private javax.swing.JButton btnSetHWStates;
    private javax.swing.JButton btnYellow;
    private javax.swing.JCheckBox chkNeedle;
    private javax.swing.JCheckBox chkPump;
    private javax.swing.JCheckBox chkReservoir;
    private javax.swing.JCheckBox chkSensor;
    private javax.swing.JLabel cptCurrent;
    private javax.swing.JLabel cptCurrentTime;
    private javax.swing.JLabel cptPrev1;
    private javax.swing.JLabel cptPrev2;
    private javax.swing.JLabel cptResetTime;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JLabel lblBattery;
    private javax.swing.JLabel lblBloodSugar;
    private javax.swing.JLabel lblCurrent;
    private javax.swing.JLabel lblCurrentBattery;
    private javax.swing.JLabel lblCurrentInsulin;
    private javax.swing.JLabel lblCurrentTime;
    private javax.swing.JLabel lblDoseTime;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblInsulin;
    private javax.swing.JLabel lblInsulinDose;
    private javax.swing.JLabel lblLastDose;
    private javax.swing.JLabel lblLastDoseTime;
    private javax.swing.JLabel lblPrev1;
    private javax.swing.JLabel lblPrev2;
    private javax.swing.JLabel lblResetTime;
    private javax.swing.JButton showGraphButton;
    private javax.swing.JTextField txtBloodSugar;
    // End of variables declaration//GEN-END:variables

    public void giveDoseGag(){
        btnManualDose.setEnabled(!btnManualDose.isEnabled());
        btnManual.setEnabled(!btnManual.isEnabled());
    }
    public void resetHardwareCheckBoxes() {
            chkNeedle.setSelected(true);            
            chkPump.setSelected(true);
            chkSensor.setSelected(true);
            chkReservoir.setSelected(true);
    }

    public void displayBSL(LinkedList l){
        LinkedList<Sample> temp = l;
        String[] bsl = new String[3];
        int i=0,j=temp.size()-1;
        if(temp.isEmpty()){
            setLblCurrentInsulin("");
            lblCurrent.setText("");
            lblPrev1.setText("");
            lblPrev2.setText("");
            lblResetTime.setText("");
            lblLastDose.setText("");
            lblLastDoseTime.setText("");
        }else{
            setLblCurrentInsulin(Integer.toString(temp.getLast().getInsulinLevel()));
            while(i<3){
                while(temp.get(j).getBloodSugar() < 0)
                    j--;
                bsl[i++] = BSLFormat.format(temp.get(j--).getBloodSugar());
            }
            lblCurrent.setText(bsl[0]);
            lblPrev1.setText(bsl[1]);
            lblPrev2.setText(bsl[2]);
            if (powerOnDose) {
               lblLastDose.setText("0");
               lblLastDoseTime.setText("");
           } else if (temp.getLast().getInsulinDose() != 0){
               lblLastDose.setText(Integer.toString(temp.getLast().getInsulinDose()));
               lblLastDoseTime.setText(convertSeconds((temp.getLast().getTime()) + (TIMEZONE*3600)));
            }
        }
    }
    
    private void setProgressBar(JProgressBar pbar, int num){
        Color c = new Color(255,0,0);
        pbar.setValue(num);
        if(num > 64){
           num = ((num -64) * 255) / 36;
           c = new Color(255-num,255,0);
        }else if(num > 32){
           num = ((num-32) * 255) / 32;
           c = new Color(255,num,0);
        }
        pbar.setForeground(c); 
    }
    
    public JButton getBtnManual() {
        return btnManual;
    }

    public void setBtnManual(JButton btnManual) {
        this.btnManual = btnManual;
    }

    public JButton getBtnManualDose() {
        return btnManualDose;
    }

    public void setBtnManualDose(JButton btnManualDose) {
        this.btnManualDose = btnManualDose;
    }

    public JToggleButton getBtnPower() {
        return btnPower;
    }

    public void setBtnPower(JToggleButton btnPower) {
        this.btnPower = btnPower;
    }

    public JCheckBox getChkNeedle() {
        return chkNeedle;
    }

    public void setChkNeedle(boolean b) {
        this.chkNeedle.setSelected(b);
    }        

    public JCheckBox getChkPump() {
        return chkPump;
    }

    public void setChkPump(boolean b) {
        this.chkPump.setSelected(b);
    }

    public JCheckBox getChkReservoir() {
        return chkReservoir;
    }

    public void setChkReservoir(boolean b) {
        this.chkReservoir.setSelected(b);
    }

    public JCheckBox getChkSensor() {
        return chkSensor;
    }

    public void setChkSensor(boolean b) {
        this.chkSensor.setSelected(b);
    }


    public void setLblBattery(String s) {
        this.lblBattery.setText(s);
    }

    public String getLblBloodSugar() {
        return lblBloodSugar.getText();
    }

    public void setLblBloodSugar(String lblBloodSugar) {
        this.lblBloodSugar.setText(lblBloodSugar);
    }

    public String getLblCurrent() {
        return lblCurrent.getText();
    }

    public void setLblCurrent(String s) {
        this.lblCurrent.setText(s);
    }

    public String getLblCurrentBattery() {
        return lblCurrentBattery.getText();
    }

    public void setLblCurrentBattery(String s) {
        this.lblCurrentBattery.setText(s);
        if (!s.equals("")) {
            int i = Integer.parseInt(s);
            setProgressBar(BatteryBar, i);
        }else {
            BatteryBar.setValue(0);
        }
    }

    public String getLblCurrentInsulin() {
        return lblCurrentInsulin.getText();
    }

    public void setLblCurrentInsulin(String s) {
        this.lblCurrentInsulin.setText(s);
        if (!s.equals("")) {
            int i = Integer.parseInt(s);
            setProgressBar(InsulinBar, i);
        }
        else 
            InsulinBar.setValue(0);
    }

    public String getLblCurrentTime() {
        return lblCurrentTime.getText();
    }

    public void setLblCurrentTime(String s) {
        this.lblCurrentTime.setText(s);
    }

    public String getLblError() {
        return lblError.getText();
    }

    public void setLblError(String lblError) {
        this.lblError.setText(lblError);
    }


    public String getLblPrev1() {
        return lblPrev1.getText();
    }

    public void setLblPrev1(String lblPrev1) {
        this.lblPrev1.setText(lblPrev1);
    }

    public String getLblPrev2() {
        return lblPrev2.getText();
    }

    public void setLblPrev2(String lblPrev2) {
        this.lblPrev2.setText(lblPrev2);
    }
    public String getLblResetTime() {
        return lblResetTime.getText();
    }

    public void setLblResetTime(String s) {
        this.lblResetTime.setText(s);
    }


    public JTextField getTxtBloodSugar() {
        return txtBloodSugar;
    }

    public void setTxtBloodSugar(JTextField txtBloodSugar) {
        this.txtBloodSugar = txtBloodSugar;
    }

  
    //two classes for creating a graph of the blood sample data
    private class BSLGraph extends JFrame {

            private theGraph graph;
            private Color aBluishColor = new Color(233, 239, 248);
            
        public BSLGraph() {
            graph = new theGraph();
            this.add(graph);
            this.setSize(400, 420);
            this.setVisible(true);
            this.setLocation(400, 400);
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            this.setBackground(aBluishColor);
        }
    }
        
    private class theGraph extends JPanel implements Runnable {  
        
           private Color aBluishColor = new Color(233, 239, 248);
           private Color reddish = new Color(217, 47, 47); 
           private Color orange = new Color(255, 115, 20);   
           private Color yellow = new Color(255, 255, 70);           
           private Color green = new Color(80, 160, 20);           
           private Color black = new Color(0, 0, 0);
           LinkedList<Sample> graphSamples = new LinkedList<Sample>();
           Thread runner;
           
        public theGraph () {
            graphSamples = callBack.getSamples();
            this.setBackground(aBluishColor);
            this.setSize(400, 400);
            this.setVisible(true);
            runner = new Thread(this);
            runner.start();
        }
        
        
        public void paintComponent(Graphics comp) {
            graphSamples = callBack.getSamples();
            Graphics2D comp2D = (Graphics2D) comp;
            comp2D.setColor(black);
            comp2D.drawLine(20, 380, 380, 380);    //x-axis 
            comp2D.drawLine(20, 20, 380, 20);
            comp2D.drawLine(20, 380, 20, 20);      //y-axis
            comp2D.drawLine(380, 380, 380, 20);
            
            //comp2D.setColor(green);
            for (int i = 0; i < 36; i++) {         //grid marks
                comp2D.drawLine(20,(370 - (i * 10))  , 25, (370 - (i * 10)) );
                comp2D.drawLine((i * (10) + 20),380, (i * (10) + 20), 375);
            }
            
            comp2D.setColor(reddish);
            comp2D.drawLine(20, 350, 380, 350);    //BSL of 3 (red)
            comp2D.drawLine(20, 310, 380, 310);    //BSL of 7 (red)
            
            //the algorithm for drawing the points...
            comp2D.setColor(green);
            try{
                if(!graphSamples.isEmpty()) {  
                    int x, y, scrollable = 0;
                    if (graphSamples.size() > 35) scrollable = (graphSamples.size() - 35);     
                    float theBSL = graphSamples.get(scrollable).getBloodSugar();
                    int prevx = 25;
                    int prevy = (int)(380 - (theBSL * 10));
                    for(int i = 0, j = (0 + scrollable); j < graphSamples.size() && i < graphSamples.size(); i++, j++) {
                        if(graphSamples.get(j).getBloodSugar()>=0){
                            theBSL = graphSamples.get(j).getBloodSugar();
                            x = ((i * 10) + 25);
                            y = (int)(380 - (theBSL * 10));
                            comp2D.drawLine(prevx, prevy , x, y);
                            prevx=x;
                            prevy=y;
                            theBSL = graphSamples.get(j).getBloodSugar();                        
                        } 
                    }
                }
            }catch(NullPointerException e){
                ;//do nothing
            }
        }

        public void run() {
            Thread thisThread = Thread.currentThread();
	    while (runner == thisThread) {
                try {
                    Thread.sleep(5);
                    graphSamples = callBack.getSamples();
                    this.repaint();
                } catch (InterruptedException ex) {
                    Logger.getLogger(SimulatorGUI1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
